#coding=utf8
import itchat
import os
import requests
import json

def tuling_auto_reply(uid, msg):
 tuling_key="7e9377d760274b3499f6dec8eed37bbb"
 if tuling_key:
  url = "http://www.tuling123.com/openapi/api"
  user_id = uid.replace('@', '')[:30]
  body = {'key': tuling_key, 'info': msg.encode('utf8'), 'userid': user_id}
  r = requests.post(url, data=body)
  respond = json.loads(r.text)
  result = ''
  if respond['code'] == 100000:
   result = respond['text'].replace('<br>', '  ')
   result = result.replace(u'\xa0', u' ')
  elif respond['code'] == 200000:
   result = respond['url']
  elif respond['code'] == 302000:
   for k in respond['list']:
    result = result + u"【" + k['source'] + u"】 " + \
             k['article'] + "\t" + k['detailurl'] + "\n"
  else:
   result = respond['text'].replace('<br>', '  ')
   result = result.replace(u'\xa0', u' ')

  print('ROBOT:', result)

  return result
 else:
  return u"知道啦"
itchat.auto_login() #热启动你的微信
# itchat.run()
# rooms=itchat.get_chatrooms(update=True)
# for i in range(len(rooms)):
#  print(rooms[i]) #查看你多有的群

room = itchat.search_friends(name=r'万里千寻') #这里输入你好友的名字或备注。
print(room)
userName = room[0]['UserName']
# f=os.getcwd()+'/img/tenor.gif' #图片地址
f='https://media.tenor.com/images/fb036d445f822ec3a106634c3b25c97c/tenor.png' #图片地址
print(f)
try:
 # itchat.send('@img@%s' % f, toUserName="filehelper")
 itchat.send_image(f, toUserName="filehelper") #如果是其他文件可以直接send_file
 # itchat.send_video(f, toUserName="filehelper") #如果是其他文件可以直接send_file
 # itchat.send_file(f, toUserName="filehelper") #如果是其他文件可以直接send_file
 # itchat.send_msg('test', toUserName=userName) #如果是其他文件可以直接send_file
 print("success")
except:
 print("fail")